package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import open.dto.PackageDS;
import open.dto.ShipmentDispatchDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货，非美国地区
 * <p>
 * cy.shipment.dispatch
 * /fulfillment/202309/packages/ship （发货，非美国地区） https://partner.tiktokshop.com/docv2/page/650aa3ebbace3e02b75d8bd4?external_id=650aa3ebbace3e02b75d8bd4#Back%20To%20Top
 * </p>
 *
 * @author gywang
 * @since 2024-07-22
 */
@Slf4j
@Service
public class TiktokShipmentDispatchService extends AbstractBaseTiktokApiHandler<ShipmentDispatchDto> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.ORDER_FULFILLMENT_PACKAGES_SHIP;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.SHIPMENT_ORDERS_DISPATCH;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentDispatchDto shipmentDispatchDTO) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        Map<String, Object> reqBody = new HashMap<>();
        List<Map<String, Object>> packList = new ArrayList<>();
        reqBody.put("packages", packList);

        for (PackageDS pack : shipmentDispatchDTO.getPacks()) {
            ShipmentDispatchDto.TiktokParameter tp = pack.getTiktokParameter();
            if (tp == null)
                tp = shipmentDispatchDTO.getTiktokParameter();
            Map<String, Object> tkPack = new HashMap<>();
            packList.add(tkPack);
            if (tp != null) {
                String handover_method = tp.getHandoverMethod();
                if (StringUtils.isNotBlank(handover_method))
                    tkPack.put("handover_method", handover_method);
                if (StringUtils.equalsIgnoreCase(handover_method, "PICKUP") && tp.getStartTime() != null && tp.getEndTime() != null) {
                    Map<String, Long> pickupSlot = new HashMap<>();
                    pickupSlot.put("start_time", tp.getStartTime().toEpochSecond(ZoneOffset.of("+8")));
                    pickupSlot.put("end_time", tp.getEndTime().toEpochSecond(ZoneOffset.of("+8")));
                    tkPack.put("pickup_slot", pickupSlot);
                }
            }

            if (StringUtils.isNotBlank(pack.getWaybillCode())) {
                Map<String, String> selfShipment = new HashMap<>();
                selfShipment.put("tracking_number", pack.getWaybillCode());
                selfShipment.put("shipping_provider_id", pack.getCarrierCode());
                tkPack.put("self_shipment", selfShipment);
            }

        }

        return this.executeTiktokApi(getDefaultUrl(platformApiParamDTO.getStoreConfig()), platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
    }
}
